package com.example.controller;

import com.example.config.ConfigBean;
import com.example.domain.Grade;
import com.example.domain.Student;
import com.example.domain.User;
import com.example.service.GradeService;
import com.example.service.StudentService;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
public class UserController {

    @Autowired
    ConfigBean configBean;
    @Autowired
    UserService userService;
    @Autowired
    GradeService gradeService;
    @Autowired
    StudentService studentService;

    @RequestMapping("/")
    public String hexo(){
        return configBean.getName()+configBean.getWant();
    }

    @RequestMapping("/add")
    public String add(){
        User user = new User();
        user.setUserName("xy2");
        user.setPassword("123456");
        userService.add(user);
        return "success";
    }

    @RequestMapping("/query/{id}")
    public String add(@PathVariable @NotNull Long id){
        List<User> userList = userService.queryUserById(id);
        if(userList!=null&&userList.size()>0){
            return userList.get(0).getUserName();
        }
        return null;
    }

    @RequestMapping("/getAllGrade")
    public String getAllGrade(){
        List<Grade> grades = gradeService.queryGradeList();
        for (Grade g:grades){
            System.out.println("id："+g.getId());
            System.out.println("班级："+g.getName());
            System.out.println("楼层："+g.getFloor());
            for(Student s:g.getStudents()){
                System.out.println("\t id："+s.getId());
                System.out.println("\t 姓名："+s.getName());
                System.out.println("\t 性别："+s.getSex());
                System.out.println("\t 年龄："+s.getAge());
            }
        }
        return null;
    }

    @RequestMapping("/getAllStudent")
    public String getAllStudent(){
        List<Student> students = studentService.queryStudentList();
            for(Student s:students){
                System.out.println("id："+s.getId());
                System.out.println("姓名："+s.getName());
                System.out.println("性别："+s.getSex());
                System.out.println("年龄："+s.getAge());
                System.out.println("班级id："+s.getGrade().getId());
                System.out.println("班级："+s.getGrade().getName());
                System.out.println("楼层："+s.getGrade().getFloor());
            }
        return null;
    }

}